﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;
using WHC.OrderWater.Commons;
using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Maticsoft.DBUtility;

namespace BYManageInfoSystem
{
    public partial class Frm_OperLog : DockContent
    {
        private DGBY.BLL.DG_OperLogTab m_bllDG_OperLogTab = new DGBY.BLL.DG_OperLogTab();
       
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;
        private Dictionary<string, string> m_dicColName2AliasName = new Dictionary<string, string>();
        private Dictionary<string, string> m_dicColAliasName2Name = new Dictionary<string, string>();
        private string m_filter = "";

        public Frm_OperLog()
        {
            InitializeComponent();
        }


        private void ShowAllRecordInDataGridView(string strWhere)
        {
            BindData(strWhere);
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            m_filter = "";
            ShowAllRecordInDataGridView(m_filter);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }
        private void SearchResult()
        {
            this.pager1.CurrentPageIndex = 1;
            m_filter = "";
            int nIndex = this.ultraGrid1.ActiveRow.Index;
            List<object> lstO = this.ultraGrid1.DisplayLayout.Bands[0].ColumnFilters.All.ToList();

            string strColumnName;
            string strColumnValue;
            SearchCondition condition = new SearchCondition();
            foreach (object o in lstO)
            {
                ColumnFilter cf = (ColumnFilter)o;
                strColumnName = cf.Column.Key;
                strColumnValue = cf.FilterConditions.Count > 0 ? ((FilterCondition)cf.FilterConditions.All[0]).CompareValue.ToString() : "";
                if (strColumnValue != "")
                {
                    condition = condition.AddCondition(m_dicColAliasName2Name[strColumnName], strColumnValue, SqlOperator.Equal);
                }
            }

            m_filter = condition.BuildConditionSql();
            m_filter = m_filter.Replace("Where", "");
            ShowAllRecordInDataGridView(m_filter);
        }
        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchResult();
            }
          
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }

        }
      

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
        }

     

        public string SqlValueList(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            string strTotal = ds.Tables[0].Rows[0]["Total"].ToString();

            return strTotal;

        }

        /// <summary>    
        /// 执行SQL查询语句，返回所有记录的DataTable集合。    
        /// </summary>    
        /// <param name="sql">SQL查询语句</param>    
        /// <returns></returns>    
        public DataTable SqlTable(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            return ds.Tables[0];

        }

        private DataTable FindToDataTable(string where, PagerInfo pagerInfo)
        {
            WHC.Pager.WinControl.PagerHelper helper = new WHC.Pager.WinControl.PagerHelper(@"select DG_OperLogTabID
,T_ACL_User.Name
,CONVERT(CHAR(19), OperDate, 20) OperDate
,OperName 
,ModuleName 
,DanjuNameOrNo from DG_OperLogTab
 join T_ACL_User on T_ACL_User.ID = DG_OperLogTab.UserPasswordTabid ", "*", "DG_OperLogTabID", pagerInfo.PageSize, pagerInfo.CurrenetPageIndex, true, where);
            string countSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.SqlServer, true);
            string dataSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.SqlServer, false);

            string value = SqlValueList(countSql);
            pagerInfo.RecordCount = Convert.ToInt32(value);//为了显示具体的信息，需要设置总记录数
            DataTable dt = SqlTable(dataSql);
            this.pager1.InitPageInfo(pagerInfo.RecordCount, pagerInfo.PageSize);
            return dt;
        }

        private void BindData(string strWhere)
        {
            this.ultraGrid1.DataSource = null;
            DataTable dt = FindToDataTable(strWhere, this.pager1.PagerInfo);





            //DataSet ds = m_bllDG_OperLogTab.GetList(strWhere);
            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                dt.Columns[strTemp].ColumnName = m_dicColName2AliasName[strTemp];

            }
            this.ultraGrid1.DataSource = dt;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;


            this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
        }

        void pager1_PageChanged(object sender, EventArgs e)
        {
            BindData(m_filter);
        }

        void pager1_ExportCurrent(object sender, EventArgs e)
        {
            MessageUtil.ShowTips("导出当前页");
        }

        void pager1_ExportAll(object sender, EventArgs e)
        {
            MessageUtil.ShowTips("导出所有");
        }      
  
        private void Frm_ClassRoom_Load_1(object sender, EventArgs e)
        {
            this.pager1.PageChanged += new WHC.Pager.WinControl.PageChangedEventHandler(pager1_PageChanged);
            this.pager1.ExportCurrent += new WHC.Pager.WinControl.ExportCurrentEventHandler(pager1_ExportCurrent);
            this.pager1.ExportAll += new WHC.Pager.WinControl.ExportAllEventHandler(pager1_ExportAll);

            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
            m_dicColName2AliasName.Add("Name", "用户名");
            m_dicColName2AliasName.Add("OperDate", "操作日期");
            m_dicColName2AliasName.Add("OperName", "操作");
            m_dicColName2AliasName.Add("ModuleName", "模块名称");
            m_dicColName2AliasName.Add("DanjuNameOrNo", "对象名称");

            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                m_dicColAliasName2Name.Add(m_dicColName2AliasName[strTemp], strTemp);
            }
        }

    }
}

